#include "pane.h"
#include <QVBoxLayout>
#include <QLabel>

QFrame* makePane(QWidget* parent,
                 const QString& name,
                 const QString& title,
                 const QColor& bodyColor,
                 const QColor& titleColor,
                 int titleHeight,
                 int bodyAlpha,
                 int titleFontPx,
                 const QString& titleBgCss)
{
    auto frame = new QFrame(parent);
    frame->setObjectName(name);
    frame->setFrameStyle(QFrame::NoFrame);

    // 整个容器布局
    auto vbox = new QVBoxLayout(frame);
    vbox->setContentsMargins(0,0,0,0);
    vbox->setSpacing(0);

    // 如果需要标题栏
    if (!title.isEmpty()) {
        auto* lbl = new QLabel(title, frame);
        lbl->setObjectName(name + "_title");          // 便于后续 findChild 精确定位
        lbl->setAlignment(Qt::AlignLeft);

        titleFontPx = qBound(8, titleFontPx, 64);

        const QString bgCss = titleBgCss.isEmpty()
                              ? titleColor.name()
                              : titleBgCss;

        lbl->setStyleSheet(QString(
            "background:%1;"
            "color:white;"
            "font-weight:bold;"
            "font-size:%2px;"
            "padding:4px;"
        ).arg(bgCss).arg(titleFontPx));

        lbl->setFixedHeight(titleHeight);
        vbox->addWidget(lbl);
    }


    // 内容区
    auto* body = new QWidget(frame);
    body->setStyleSheet(QString(
        "background: rgba(%1,%2,%3,%4);"
        "border:1px solid rgba(0,0,0,40);"
        "border-top: 0;"
        "border-right: 0;"
    ).arg(bodyColor.red())
     .arg(bodyColor.green())
     .arg(bodyColor.blue())
     .arg(qBound(0, bodyAlpha, 255)));  // 避免越界

    vbox->addWidget(body, 1);   // 占满剩余空间
    return frame;
}
